# -*- encoding:utf-8 -*-
import requests
from constant import constant_define
from dao.MySqlDAL import MySqlDAL
import  time
from config.Config import Config
import utils.CookieUtils as cu
__author__ = 'shudong.msd'

'''
    抓取流通市值
'''
headers = {
        'Accept':'application/json, text/javascript, */*; q=0.01',
        'Accept-Encoding':'gzip, deflate, sdch',
        'Accept-Language':'zh-CN,zh;q=0.8,en;q=0.6',
        'cache-control':'no-cache',
        'Connection':'keep-alive',
        'User-Agent': constant_define.user_agent,
        'Referer':'http://xueqiu.com/hq/screener/CN',
        'Host':'xueqiu.com',
        'X-Requested-With':'XMLHttpRequest',
        'Cookie': cu.init_cookie()
}

# 降序
target_url_1 = 'http://xueqiu.com/stock/screener/screen.json?category=SH&exchange=&areacode=&indcode=&orderby=fmc&order=desc&current=ALL&pct=ALL'
# 升序
target_url_2 = 'http://xueqiu.com/stock/screener/screen.json?category=SH&exchange=&areacode=&indcode=&orderby=fmc&order=asc&current=ALL&pct=ALL'

def init_stock_mv():
    global target_url_1
    global target_url_2
    global headers

    #  只能升序抓到前1192条，降序抓到1191条  各执行一次
    req_asc = requests.get(target_url_2,headers=headers,params={'size':1135}).json()
    time.sleep(0.5)
    req_desc = requests.get(target_url_1,headers=headers,params={'size':1135}).json()

    stock_list = list()

    for item in req_asc['list']:
        tmp = (item['fmc'],time.time(),item['symbol'])
        if tmp in stock_list:
            continue
        stock_list.append(tmp)

    for item in req_desc['list']:
        tmp = (item['fmc'],time.time(),item['symbol'])
        if tmp in stock_list:
            continue
        stock_list.append(tmp)

    stock_list.sort()

    print len(stock_list)


    #插入db
    sqlDao = MySqlDAL()

    data_keys = ['market_value','update_time']
    filter_collection_key = ['stock_id']

    sqlDao.update_many_batch(data_keys,stock_list,'tb_stock_info',filter_collection_key,80)

